

import 'package:flutter_cache_manager/flutter_cache_manager.dart';

/// flutter_cache_manager 封装
class FileCacheManager extends CacheManager with ImageCacheManager  {

  static const key = 'libCachedFileDataKey';

  static CacheManagerLogLevel logLevel = CacheManagerLogLevel.debug;

  static final FileCacheManager instance = FileCacheManager._();

  factory FileCacheManager() {
    return instance;
  }

  FileCacheManager._() : super(Config(
    key,
    stalePeriod: const Duration(days: 1),
    maxNrOfCacheObjects: 20,
    repo: JsonCacheInfoRepository(databaseName: key),
    fileService: HttpFileService(),
  ));

}


extension CacheManagerExtension on CacheManager {

  /// 只下载文件，不进行缓存
  Future<FileInfo> onlyDownloadFile(String url,
      {String? key,
        Map<String, String>? authHeaders,
        bool force = false}) async {

    key ??= url;
    final fileResponse = await webHelper
        .downloadFile(
      url,
      key: key,
      authHeaders: authHeaders,
      ignoreMemCache: force,
    )
        .firstWhere((r) => r is FileInfo);
    return fileResponse as FileInfo;
  }

}